﻿
Imports std
Imports storoomc

Partial Public Class storoomOper
    Protected Function shareTypeFull(ByVal slices As vector(Of storageInterface)) As ternary
        Return threadModel().multiOper(d(Function(ByRef s As storageInterface) As ternary
                                             Return Not s.full()
                                         End Function),
                                       slices).reverse()
    End Function

    Protected Function shareTypeCapacity(ByRef cap As UInt64,
                                         ByVal slices As vector(Of storageInterface)) As Boolean
        cap = maxUInt64
        Return threadModel().multiOper(d(Function(ByRef s As storageInterface, ByRef this As UInt64) As ternary
                                             Return s.capacity(this)
                                         End Function),
                                       Function(ByRef this As UInt64, ByRef total As UInt64) As Boolean
                                           If total > this Then
                                               total = this
                                           End If
                                           Return True
                                       End Function,
                                       slices, cap)
    End Function

    Protected Function randomSelectModify(ByVal key As String, ByVal buff As bytes,
                                          ByRef slice As storageInterface,
                                          ByVal slices As vector(Of storageInterface)) As Boolean
        If slices Is Nothing OrElse slices.size() = 0 Then
            Return writeImpl(key, buff, slice, Me.slices)
        Else
            Dim i As Int64
            i = rnd(0, slices.size(), True)
            slice = slices(i)
            slices.erase(i)
            Return (slices.size() = 0 OrElse deleteImpl(key, slices)) AndAlso slice.modify(key, buff)
        End If
    End Function
End Class
